<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
use app\common\model\Store;
use app\common\model\User;
// use think\facade\Session;

class ApiBase extends ApiTop
{
    protected $uid;
    public function initialize(){
        // 判断用户是否登录
        $header = request()->header();

        if(!isset($header['token']) || empty($header['token'])){
            json(['status'=> 10011,'msg'=> '登录失效'])->send();
            exit;
        }

        if(!cache($header['token'])){
            json(['status'=> 10011,'msg'=> '登录失效'])->send();
            exit;
        }

        $this->uid = cache($header['token']);
    }

    // 判断店铺是否过期，是否禁用
    public function storeIsNormal($uid)
    {
        $user = new User();
        $userList =   $user->getOne(['id' =>$uid], 'id,store_id');
        if(empty($userList['store_id'])){
            $data['code'] = 1;
            $data['msg'] = "请先绑定店铺，或者激活年卡";
            return $data;
        }


        $storeList = $this->Store->getOne(['id'=>$userList['store_id']],"id,expiration_time,status");


        if(empty($storeList)){
            $data['code'] = 1;
            $data['msg'] = "请先绑定店铺，或者激活年卡";
            return $data;
        }

        if($storeList['status'] == 1){
            $data['code'] = 1;
            $data['msg'] = "店铺已经被禁用，请联系客服";
            return $data;
        }

        if($storeList['expiration_time'] <= time()){
            $data['code'] = 1;
            $data['msg'] = "店铺vip已过期，请续费";
            return $data;
        }

        $data['code'] = 0;
        $data['store_id'] = $userList['store_id'];
        $data['msg'] = "正常店铺";

        return $data;

    }
}
